package com.hugesoft.jxgs.activity.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.StartActivity;

public class BaseActivity extends Activity {
	private static WindowManager mWindowMgr = null;
	private WindowManager.LayoutParams mWindowMgrParams = null;
	private ProgressDialog mDialog;
	private Vibrator mVibrator;
	public static final String FRIENDCOMBINETAG = "friendcombinetag";
	public static final String GETUSERINFORSPTAG = "GETUSERINFORSPTAG";
	public BroadcastReceiver mBroadcastReceiver;
	public int mScreenWidth, mScreenHeight;
	private static final String TAG = "BaseActivity";

	public void addIconToStatusbar(int resId, int notiType, String tipContent) {
		NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		Notification n = new Notification(resId, tipContent,
				System.currentTimeMillis());
		n.flags |= Notification.FLAG_ONGOING_EVENT;
		n.flags |= notiType;
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setClass(this, StartActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
		n.contentIntent = pi;
		if (true) {
			n.defaults = Notification.DEFAULT_SOUND;
		}
		n.setLatestEventInfo(this, "江西高速", "", pi);
		if (false) {
			mVibrator.vibrate(500);
		}
		nm.notify(999, n);
	}

	private void initParams() {
		DisplayMetrics dm = getResources().getDisplayMetrics();
		mWindowMgrParams.x = dm.widthPixels - 136;
		mWindowMgrParams.y = 300;
		mWindowMgrParams.width = 136;
		mWindowMgrParams.height = 136;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

		mScreenWidth = displayMetrics.widthPixels;
		mScreenHeight = displayMetrics.heightPixels;
		if (mVibrator == null) {
			mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		}
	}

	public void deleteIconToStatusbar() {
		NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		nm.cancel(999);
	}

	public void backToDesk() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
		deleteIconToStatusbar();
		System.exit(0);
	}

	public void showWaitDialog(String content) {
		mDialog = new ProgressDialog(BaseActivity.this);
		mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mDialog.setTitle("请稍候");
		mDialog.setMessage(content);
		mDialog.setIcon(R.drawable.ic_launcher);
		mDialog.setCancelable(true);
		mDialog.show();
	}

	public void showWaitDialogNoCancel(String content) {
		mDialog = new ProgressDialog(BaseActivity.this);
		mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mDialog.setTitle("请稍候");
		mDialog.setMessage(content);
		mDialog.setIcon(R.drawable.ic_launcher);
		mDialog.setCancelable(false);
		mDialog.show();
	}

	public void dismissDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
	}

	public void showAlertDialog(String title, String message, String leftText,
			String rightText, final LeftButtonclickListener leftClick,
			final RightButtonclickListener rightClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(leftText,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (leftClick != null) {
							leftClick.onLeftBtnClick();
						}
					}
				});
		builder.setNegativeButton(rightText,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (rightClick != null) {
							rightClick.onRightBtnClick();
						}
					}
				});
		builder.create().show();
	}

	public interface LeftButtonclickListener {
		public void onLeftBtnClick();
	}

	public interface RightButtonclickListener {
		public void onRightBtnClick();
	}
}
